package handler

import (
	"errors"
	"net/http"
	common "zhongxc.cc/mall-common"
	"zhongxc.cc/mall-common/errs"

	"github.com/zeromicro/go-zero/rest/httpx"
	"zhongxc.cc/mall-product-api/internal/logic"
	"zhongxc.cc/mall-product-api/internal/svc"
	"zhongxc.cc/mall-product-api/internal/types"
)

type ProductHandler struct {
	svcCtx *svc.ServiceContext
}

func NewProductHandler(svcCtx *svc.ServiceContext) *ProductHandler {
	return &ProductHandler{
		svcCtx: svcCtx,
	}
}

func (p *ProductHandler) ProductInfo(w http.ResponseWriter, r *http.Request) {
	var req types.InfoRequest
	if err := httpx.ParseForm(r, &req); err != nil {
		httpx.ErrorCtx(r.Context(), w, err)
		return
	}
	result := common.NewResult()
	l := logic.NewProductLogic(r.Context(), p.svcCtx)
	resp, err := l.ProductInfo(&req)
	code, msg := errs.ParseGrpcError(err)
	httpx.OkJsonCtx(r.Context(), w, result.Ok(resp, code, errors.New(msg)))
}
